package org.bird.sso.server.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bird.sso.server.JVMCache;

public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = -8006972436827751301L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String service = request.getParameter("service");

		if ("admin".equals(username) && "admin".equals(password)) {
			Cookie cookie = new Cookie("sso", username);
			cookie.setPath("/");
			response.addCookie(cookie);

			long time = System.currentTimeMillis();
			String timeString = username + time;
			JVMCache.TICKET_AND_NAME.put(timeString, username);

			if (null != service) {
				StringBuilder url = new StringBuilder();
				url.append(service);
				if (0 <= service.indexOf("?")) {
					url.append("&");
				} else {
					url.append("?");
				}
				url.append("ticket=").append(timeString);
				response.sendRedirect(url.toString());
			} else {
				response.sendRedirect("/sso/login.jsp");
			}
		} else {
			response.sendRedirect("/sso/login.jsp?service=" + service);
		}
	}
}
